<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            text-align: center;
            margin: auto;
            width: 300px;
            height: 300px;
            /* border: 1px solid #ddd; */
        }
        .box1{
            width: 250px;
            height: 250px;
            margin: auto;
            border: 1px solid #ddd;
            display: flex;
            align-items: center;
            justify-content: space-around;
        }
        .box2{
            width: 50px;
            height: 50px;
            border: 1px solid #ddd;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="box">
        <button onclick="d_r()">点击将div变成绿色</button>
        <div class="box1">

            <div class="box2" ></div>
            <div class="box2"></div>
            <div class="box2"></div>
        </div>
    </div>
</body>
</html>
<script >
   var box2 = document.querySelectorAll(".box2")
   console.log(box2);
    function d_r(){
        for (var i=0;i<box2.length;i++){
            box2[i].style.background="green"
        }
    }
</script>